package com.tanhua.server.interceptor;

import com.tanhua.model.domain.User;

/**
 * 工具类：实现ThreadLocal存取数据的方法
 * @author Xiao
 * @date 2021/10/22
 */
public class UserHolder {
    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 存储user
     * @param user
     */
    public static void set(User user) {
        threadLocal.set(user);
    }

    /**
     * 获取user
     * @return
     */
    public static User get() {
        return threadLocal.get();
    }

    /**
     * 获取id
     * @return
     */
    public static Long getUserId() {
        return threadLocal.get().getId();
    }

    /**
     * 获取mobile
     * @return
     */
    public static String getMobile() {
        return threadLocal.get().getMobile();
    }

    /**
     * 清理ThreadLocal对象
     */
    public static void remove() {
        threadLocal.remove();
    }
}
